package com.tamartech.greendb.importers;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.Ostermiller.util.CSVParser;
import com.Ostermiller.util.LabeledCSVParser;
import com.tamartech.greendb.loader.CanvassLoader;

// class to initially import Jonathan's spreadsheet. Unlikely ever to need it again
public class CanvassImporter
{
    private static Logger logger = Logger.getLogger (CanvassImporter.class);
    private static SimpleDateFormat formatCanvassDate = new SimpleDateFormat ("dd/MM/yyyy");
        
    
    public static void main(String[] args) throws Exception
	{
        File inFile = new File (args[0]);
        Date date = formatCanvassDate.parse(args[1]);
        
        String contactedBy = "";
        if (args.length > 2)
            contactedBy = args[2];
        
        CanvassLoader loader = new CanvassLoader();
        loader.begin(date, contactedBy);
        
        LabeledCSVParser csv = new LabeledCSVParser (new CSVParser (new FileInputStream (inFile)));
        
        
        for (;;)
        {
            String[] line = csv.getLine();

            if (line == null)
                break;

            Integer rank = null;
            if (csv.getValueByLabel("rank").trim().length() > 0)
            	rank = Integer.valueOf(csv.getValueByLabel("rank"));
            
            loader.putContact (Long.valueOf(csv.getValueByLabel("person")),
			                   csv.getValueByLabel("tag"),
  		                       rank,
                               csv.getValueByLabel("notes"));
        }
        
        csv.close();

        loader.end();
	}	
} 